package com.catsic.web.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.core.form.ResourceForm;
import com.catsic.core.resource.service.ResourceService;

@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * create:添加
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> create(ResourceForm form){
		form = resourceService.add(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(ResourceForm form){
		form = resourceService.update(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * cancel:注销
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/cancel")
	@ResponseBody
	public Map<String,Object> cancel(String id){
		resourceService.cancel(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		resourceService.delete(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}

    /**
     * 激活
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/active")
	@ResponseBody
	public Map<String,Object> active(String id){
		resourceService.active(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过父节点ID获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/query" )
	@ResponseBody
	public Map<String,Object> query(String menuCode){
		List<ResourceForm> list = resourceService.query(menuCode);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * 通过userId获取行为,具体说明参照/common/resource.js
	 * @param userId 用户ID
	 * @param menuCode 菜单编码
	 * @param drawerCode 行为类型
	 * @param functionType 功能类型
	 * @return
	 */
	@RequestMapping(value = "/findResourcesByUserId")
	@ResponseBody
	public Map<String,Object> findResourcesByUserId(String userId,String menuCode,String drawerCode,String authAreaCode){
		List<ResourceForm> list = resourceService.findResourcesByUserId(userId,menuCode,drawerCode,authAreaCode);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
}
